package br.com.empresa.controller;

import java.io.IOException;
import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;

import javax.swing.JOptionPane;

import org.exolab.castor.xml.handlers.ValueOfFieldHandler;





import br.com.empresa.app.Principal;
//import br.com.empresa.dao.FornecedorDAO;
import br.com.empresa.dao.PatrimonioDAO;
//import br.com.empresa.modelo.Fornecedor;
import br.com.empresa.modelo.Patrimonio;
import br.com.empresa.modelo.TabelaPatrimonio;
import javafx.beans.property.IntegerProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;

public class PatrimonioController implements Initializable{

	

    @FXML
    private TableColumn clNome;
    @FXML
    private TableColumn clDtaquisicao;
    @FXML
    private TableColumn clValor;
    @FXML
    private TableColumn clCodigo;

    @FXML
    private Button btExcluir;
    @FXML
    private Button btEditar;
    @FXML
    private Button btFornecedor;
    
    @FXML
    private TableView<TabelaPatrimonio> tabelaPat;
    
    @FXML
    private TextField txProcurar;

    @FXML
    private TextField txCodigo;
    @FXML
    private TextField txNome;
    @FXML
    private TextField txCustos;
    @FXML
    private TextField txNatureza;
    @FXML
    private TextField txFornecedor;
    @FXML
    private TextField txDtaquisicao;
    @FXML
    private TextField txValor;
    @FXML
    private TextField txAtivo;

    @FXML
    private Button txSalvar;
    @FXML
    private Button txLimpar;
    

    @FXML
    void eventSalvar(ActionEvent event) {
    	Patrimonio f = new Patrimonio();
    	f.setNome(txNome.getText());
    	f.setCentroCustos(txCustos.getText());
    	f.setNatureza(txNatureza.getText());
    	f.setFornecedor(txFornecedor.getText());
    	f.setDtAquisicao(txDtaquisicao.getText());
    	f.setValor(txValor.getText());
    	f.setAtivo(txAtivo.getText());
    	PatrimonioDAO dao = new PatrimonioDAO();
    	
    	if (!txCodigo.getText().trim().equals("")){
    		f.setCodigo(Integer.parseInt(txCodigo.getText()));
    		dao.editar(f);
    	}else{
    		dao.inserir(f);
    	}
    	JOptionPane.showMessageDialog(null, "Salvo com sucesso");
    	CarregarTabela();
    	
    }

    @FXML
    void eventoLimpar(ActionEvent event) {
    	
    	txNome.setText("");
    	txCustos.setText("");
    	txNatureza.setText("");
    	txFornecedor.setText("");
    	txDtaquisicao.setText("");
    	txValor.setText("");
    	txAtivo.setText("");
    	txCodigo.setText("");
    	
    }
    
    @FXML
    void eventEditar(ActionEvent event) {
    	
    	IntegerProperty cdPatrimonio = tabelaPat.getSelectionModel().getSelectedItem().codigoProperty();
    	
    	PatrimonioDAO dao = new PatrimonioDAO();
    	Patrimonio fSalvo = dao.listarPorid(cdPatrimonio.intValue());
    	
    	txNome.setText(fSalvo.getNome());
    	txCustos.setText(fSalvo.getCentroCustos());
    	txNatureza.setText(fSalvo.getNatureza());
    	txFornecedor.setText(fSalvo.getFornecedor());
    	txDtaquisicao.setText(fSalvo.getDtAquisicao());
    	txValor.setText(fSalvo.getValor());
    	txAtivo.setText(fSalvo.getAtivo());
    	txCodigo.setText(String.valueOf(fSalvo.getCodigo()));
    	    	    	    	
    }

    @FXML
    void eventExcluir(ActionEvent event) {
    	int linhaSelecionada = tabelaPat.getSelectionModel().getSelectedIndex();
    	IntegerProperty cdPatrimonio = tabelaPat.getSelectionModel().getSelectedItem().codigoProperty();
    	PatrimonioDAO dao = new PatrimonioDAO();
    	//exclui banco
    	dao.excluir(cdPatrimonio.intValue());
    	//exclui tabela
    	tabelaPat.getItems().remove(linhaSelecionada);
    	
    }
    
    

    @FXML
    void eventPesquisar(ActionEvent event) {
    	
    	PatrimonioDAO dao = new PatrimonioDAO();
		List<Patrimonio> bens = dao.listarNome(txProcurar.getText());
		ObservableList<TabelaPatrimonio> dados = FXCollections.observableArrayList();

		for(Patrimonio f: bens){
			
			dados.add(new TabelaPatrimonio(f));
			
		}
		
		clCodigo.setCellValueFactory(new PropertyValueFactory("codigo"));
		clNome.setCellValueFactory(new PropertyValueFactory("nome"));
		clDtaquisicao.setCellValueFactory(new PropertyValueFactory("dtAquisicao"));
		clValor.setCellValueFactory(new PropertyValueFactory("valorAquisicao"));
		tabelaPat.setItems(dados);
    	
    }
    
    
    
	@Override
	public void initialize(URL arg0, ResourceBundle arg1) {
		
		CarregarTabela();
		
	}

	private void CarregarTabela() {
		PatrimonioDAO dao = new PatrimonioDAO();
		List<Patrimonio> bens = dao.listar();
		ObservableList<TabelaPatrimonio> dados = FXCollections.observableArrayList();

		for(Patrimonio f: bens){
			
			dados.add(new TabelaPatrimonio(f));
			
		}
		
		clCodigo.setCellValueFactory(new PropertyValueFactory("codigo"));
		clNome.setCellValueFactory(new PropertyValueFactory("nome"));
		clDtaquisicao.setCellValueFactory(new PropertyValueFactory("dtAquisicao"));
		clValor.setCellValueFactory(new PropertyValueFactory("valorAquisicao"));
		tabelaPat.setItems(dados);
	}
	
	
	 @FXML
	    void eventFornecedor(ActionEvent event) {

		 try{
			 
			 Parent root = FXMLLoader.load(getClass().getResource("/br/com/empresa/view/fornecedor.fxml"));
			 Principal.SCENE.setRoot(root);
		 }catch (IOException ex){
			 ex.printStackTrace();
			 
		 }
		 
	    }
	

}

